/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.base.jfinal.directive;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.naming.NameAlreadyBoundException;

import com.jfinal.kit.StrKit;
import com.jfinal.template.Directive;
import com.jfinal.template.Engine;

import cn.hg.base.jfinal.directive.annotation.JfsaDirective;
import cn.hg.base.kit.ClassLoadKit;

/**
 * @author Golly
 */
public class AutoDirectiveEngine {

	public static void register (Engine e, boolean onlyJfsaDirective, String... basePackages) {
		Set<Class<?>> classSet;

		try {

			if (onlyJfsaDirective) {
				classSet = ClassLoadKit.getClassSet(basePackages, Directive.class, JfsaDirective.class);
			} else {
				classSet = ClassLoadKit.getClassSet(basePackages, Directive.class);
			}

		}
		catch (ClassNotFoundException | IOException e1) {
			throw new RuntimeException(e1);
		}

		try {
			Map<String, Class<? extends Directive>> directiveMap =
				AutoDirectiveEngine.convert2DirectiveMap(classSet, onlyJfsaDirective);

			for (String directiveName : directiveMap.keySet()) {
				e.addDirective(directiveName, directiveMap.get(directiveName));
			}

		}
		catch (NameAlreadyBoundException e1) {
			throw new RuntimeException(e1);
		}

	}

	@SuppressWarnings ("unchecked")
	private static Map<String, Class<? extends Directive>> convert2DirectiveMap (Set<Class<?>> classSet,
		boolean onlyJfsaDirective) throws NameAlreadyBoundException {
		Map<String, Class<? extends Directive>> directiveMap = new HashMap<>();

		for (Class<?> clz : classSet) {

			if (!Modifier.isAbstract(clz.getModifiers())) {
				JfsaDirective jd = clz.getAnnotation(JfsaDirective.class);
				String directiveName = null;

				if (onlyJfsaDirective && jd == null) {
					continue;
				} else if (jd != null) {
					directiveName = jd.value();
				} else {
					directiveName = AutoDirectiveEngine.getDirectiveName(clz);
				}

				if (directiveMap.containsKey(directiveName)) {
					throw new NameAlreadyBoundException("Directive of " + directiveName + " already exist...");
				}

				directiveMap.put(directiveName, (Class<? extends Directive>) clz);
			}

		}

		return directiveMap;
	}

	private static String getDirectiveName (Class<?> clz) {
		String name = clz.getSimpleName();
		name = StrKit.firstCharToLowerCase(name);

		if (name.lastIndexOf("Directive") > 0) {
			name.subSequence(0, name.lastIndexOf("Directive"));
		}

		return name;
	}

}
